<template>
  <!-- v-loading="true" -->
  <div>
    <pageSearch :searchFormConfig="searchFormConfig" @searchSubmit="searchSubmit" />
    <pageTable :tableData="tableData" :tableInit="params.tableInit" @sizeChange="sizeChange"
      @currentChange="currentChange" @exportFn="exportExcel" v-loading="loading">
      <template #type="{ row }">
        <el-tag class="ml-2" :type="row.type == 0 ? '' : (row.type == 1 ? 'success' : 'danger')">{{ row.type == 0 ? '未发送'
          :
          (row.type == 1 ? '已发送' : '拒绝') }}</el-tag>
      </template>
      <template #operation="{ row }">
        <el-button type="primary" link @click="copyToClipboard(row)">复制邮箱</el-button>

        <el-popconfirm title="确认要删除吗?" @confirm="delData(row.id)">
          <template #reference>
            <el-button type="danger" link>删除</el-button>
          </template>
        </el-popconfirm>

      </template>



    </pageTable>

    <Dialog ref="DialogRef" @submit="DialogSubmit" @onclose="onDialogClose" title="分配业务经理"></Dialog>


  </div>
</template>

<script setup lang="ts">
import pageSearch from "@/components/base-ui/page-search";
import pageTable from "@/components/base-ui/page-table";
import Dialog from "@/components/base-ui/dialog";
import { searchFormConfig } from "./config/search.config";
import { tableInit } from "./config/table.config";
import { usepageTable } from '@/components/base-ui/page-table/usePageTable'
import { getSelmessagepage, changemessage, delmessage } from '@/api/message'
import { useRoute } from "vue-router";
import { message } from "@/utils/message";

const route = useRoute();
console.log(route, 'route')
const {
  exportExcel,
  tableData,
  getData,
  params,
  loading,
  sizeChange,
  currentChange,
  searchSubmit,
  delData,
  DialogRef,
  DialogSubmit,
  onDialogClose,
} = usepageTable({
  tableInit,
  xlsxName: '文档授权.xlsx',
  getRes: getSelmessagepage,
  delRes: delmessage,
  otherQuery: {
    id: route.query.childid ?? ''
  }
})

getData()

const copyToClipboardFn = (content) => {
  const textarea = document.createElement("textarea")
  textarea.value = content
  document.body.appendChild(textarea)
  textarea.select()
  document.execCommand("Copy")
  textarea.remove()
}

const copyToClipboard = (row) => {
  copyToClipboardFn(row.email);
  message("复制成功", {
    type: "success"
  });
  changemessage({
    "id": row.id,
    "type": 1
  }).then(res => {
    getData()
  })


}

</script>

<style scoped></style>
